package com.area.cth.controller;

import com.area.cth.mapper.DealMapper;
import com.area.cth.pojo.Deal;
import com.area.cth.common.Result;
import com.area.cth.service.DealService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "交易信息相关接口")
@RestController
@RequestMapping("/deal")
@CrossOrigin
public class DealController {
    @Autowired
    private DealMapper dealMapper;

    @Autowired
    private DealService dealService;

    @Operation(summary = "新增交易信息")
    @PostMapping
    public Result addDeal(@RequestBody @Validated Deal deal) {
        dealMapper.insert(deal);
        return Result.success();
    }

    @Operation(summary = "分页查询")
    @GetMapping("/list")
    public Result<IPage<Deal>> dealList(Integer pageNo, Integer pageSize) {
        IPage<Deal> ds = dealService.dealList(pageNo, pageSize);
        return Result.success(ds);
    }

    @Operation(summary = "查询买家个人交易信息")
    @GetMapping("/buyerMy")
    public Result<List<Deal>> getBuyerMyDealList() {
        List<Deal> ds = dealService.getBuyerMyDealList();
        return Result.success(ds);
    }

    @Operation(summary = "查询卖家个人交易信息")
    @GetMapping("/sellerMy")
    public Result<List<Deal>> getSellerMyDealList() {
        List<Deal> ds = dealService.getSellerMyDealList();
        return Result.success(ds);
    }

    @Operation(summary = "修改交易信息")
    @PutMapping
    public Result update(@RequestBody @Validated Deal d) {
        dealMapper.updateById(d);
        return Result.success();
    }

    @Operation(summary = "删除交易信息")
    @DeleteMapping
    public Result delete(Integer id) {
        dealMapper.deleteById(id);
        return new Result(0, "删除成功", null);
    }
}
